/**
 * @class Kohive.controllers.KohiveController
 * @extends ExtMVC.Controller
 * Base Kohive Controller
 */
Kohive.controllers.KohiveController = Ext.extend(ExtMVC.Controller, {

	/**
	 * @property viewCache
	 * @type Object
	 * An object which retains cached views per hive so that they can be shown again rather than
	 * re-rendered
	 */
	viewCache							: {},

	/**
	 * If a given view ID has already been rendered inside the current Hive, this will return it
	 * @param {String} id The ID of the view to retrieve
	 * @return {Ext.Component|Null} The cached view object, if found
	 */
	getViewFromCache					: function(id) {
		var cache = this.getCacheForCurrentHiveAndController();

		return cache[id];
	},

	/**
	 * Caches a given view instance keyed by current hive.
	 * @param {String} key The key to cache this component under (usually its viewname - e.g. 'index', 'new' etc)
	 * @param {Ext.Component} component A component to cache within the current hive (usually a Kohive.Window)
	 */
	addToViewCache						: function(key, component) {
		var cache = this.getCacheForCurrentHiveAndController();

		cache[key] = component;
	},

	/**
	 * Function description
	 */
	saveViewsForCurrentHive				: function() {
		var controllers = this.getCacheForCurrentHive();

		for (var c in controllers) {
			var views = controllers[c];

			for (var v in views) {
				var view = views[v];

				// The controller can have strange 'refresh' views (maybe others, too), which aren't valid views
				if (view) {
					view.relaunch = !view.hidden;
					view.hide();
				}
			}
		}
	},

	/**
	 * For every view cached in the currently loaded hive, runs show()
	 */
	relaunchViewsForCurrentHive			: function() {
		var controllers = this.getCacheForCurrentHive();

		for (var c in controllers) {
			var views = controllers[c];

			for (var v in views) {
				if (views[v] != undefined && views[v].relaunch)
					views[v].show();
			}
		}

	},

	/**
	 * Internal method, returns the component cache for the current hive and creates it if not already present
	 * @return {Object} The cache object for the current hive
	 */
	getCacheForCurrentHive				: function() {
		var membership = Kohive.OS.getCurrentHive();
		if (membership) {
			var hiveId = membership.get('hive_id');

			this.viewCache[hiveId] = this.viewCache[hiveId] || {};

			return this.viewCache[hiveId];
		} else
			return {};
	},

	/**
	 * Internal method, returns the component cache for the current hive AND controller and creates it if not already present
	 * @return {Object} The cache object for the current hive/controller combination
	 */
	getCacheForCurrentHiveAndController	: function() {
		var membership = Kohive.OS.getCurrentHive();
		if (membership) {
			var hiveId = membership.get('hive_id');

			this.viewCache[hiveId] = this.viewCache[hiveId] || {};
			this.viewCache[hiveId][this.name] = this.viewCache[hiveId][this.name]
					|| {};

			return this.viewCache[hiveId][this.name];
		} else
			return {};
	},

	/**
	 * Renders the view by calling the superclass. Also caches this view onto the current controller's cache, allowing us to relaunch
	 * views when we switch hives.  By default, each view is cached with the viewName as the cache key.  To override this, provide a
	 * viewCacheKey property in the config object and this will be used again.  Use this whenever you need to use the same view more 
	 * than once.  e.g. to set a cache key for the edit screen of object with ID = 34 you might do this:
	 * this.renderView('edit', {viewCacheKey: 'edit-34'})
	 * @param {String} viewName The name of the view to render. This is translated into the appropriate view in this controller's view namespace
	 * @param {Object} config A config object passed to the view constructor.  Provide a viewCacheKey to set a custom view cache key
	 */
	renderView							: function(viewName, config) {
		var config = config || {}, key = config.viewCacheKey || viewName, view = this
				.getViewFromCache(key);

		if (view == undefined) {
			// call super and add to cache
			var view = Kohive.controllers.KohiveController.superclass.renderView
					.apply(this, arguments);
			this.addToViewCache(key, view);
		}

		if (view)
			view.show();

		return view;
	},

	/**
	* TODO: A hacky method to prepend the /memberships/:membership_id/ segment to the front of an AJAX
	* request url.  This really ought to be handled by the model, so factor it out of here when possible
	* @param {String} url The url to append to the memberships scope
	* @return {String} The fully scoped url
	*/
	scopedUrl							: function(url) {
		return String.format("/memberships/{0}/{1}", Kohive.OS.getCurrentHive()
						.get('id'), url);
	}
}
);